

clear

use "F:\index_wrds.dta" 
drop _NAME_ IndNm
rename COL1 ret
gen ret2=ret*100
drop ret
rename ret2 ret

rename MthCalDt date

reshape wide ret,i(date) j(INDNO)
rename ret1000000 nyse
rename ret1000060 nasdaq
rename ret1000510 sp
keep if nasdaq~=.

gen longshort=(nasdaq/100)-(nyse/100)
gen longshort_index=1

univar nyse nasdaq sp
corr nyse nasdaq sp

gen nyse_index=1
gen nasdaq_index=1
gen sp_index=1

gen retplusone=(1+nyse/100)
sort date
gen double product = retplusone[1]
   replace product = product[_n-1] * retplusone if _n > 1
   replace nyse_index=nyse_index[_n-1]*retplusone if _n > 1
   replace product = product[_N]
   replace product=product-1
rename product life_nyse
drop retplusone

gen retplusone=(1+nasdaq/100)
sort date
gen double product = retplusone[1]
   replace product = product[_n-1] * retplusone if _n > 1
   replace nasdaq_index=nasdaq_index[_n-1]*retplusone if _n > 1
   replace product = product[_N]
   replace product=product-1
rename product life_nasdaq
drop retplusone

gen retplusone=(1+sp/100)
sort date
gen double product = retplusone[1]
   replace product = product[_n-1] * retplusone if _n > 1
   replace sp_index=sp_index[_n-1]*retplusone if _n > 1
   replace product = product[_N]
   replace product=product-1
rename product life_sp
drop retplusone

gen retplusone=(1+longshort)
sort date
gen double product = retplusone[1]
   replace product = product[_n-1] * retplusone if _n > 1
   replace longshort_index=longshort_index[_n-1]*retplusone if _n > 1
   replace product = product[_N]
   replace product=product-1
rename product life_longshort
drop retplusone


gen lnnyse=ln(nyse_index)
gen lnnasdaq=ln(nasdaq_index)
gen lnsp=ln(sp_index)

tsset date
/*tsline lnnyse lnnasdaq lnsp*/
gen month=month(date)
gen year=year(date)
sort year month
save index,replace

clear
summ
/*
insheet using "F:\Dropbox\Research\Q-Hacking-Project\Hank\allfactors.csv"
*/
insheet using "F:\Dropbox\Research\Q-Hacking-Project\Hank\themes.csv"

keep if year>1972
sort year month

merge m:1 year month using index
sort name
drop _merge
replace ret=ret*100

save factors,replace

/* Estimate average returns */
statsby _b _se,by(name) clear :reg ret
sort name
rename _b_cons raw_cons
rename _se_cons raw_se

save factor_results_raw,replace


clear
use factors
keep if year>1972

statsby _b _se e(r2),by(name) clear :reg ret longshort
sort name
save factor_results,replace
clear
use factors
keep if year>1972

merge m:1 name using factor_results
drop _merge
merge m:1 name using factor_results_raw

rename _eq2_stat_1 r2
gen t_cons_raw=raw_cons/raw_se
gen t_cons=_b_cons/_se_cons

summ r2 raw_cons _b_cons

save temp1,replace


collapse (mean) t_cons_raw t_cons r2 raw_cons _b_longshort _b_cons _se_longshort _se_cons (sd) sd_ret=ret,by(name)
summ
c














